﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace CommunitySystem.Ctrls
{
    public class ImageButton : Button
    {
        public ImageButton()
            : base()
        {
            this.DefaultStyleKey = typeof(ImageButton);
            this.Loaded += new RoutedEventHandler(ImageButton_Loaded);
        }

        void ImageButton_Loaded(object sender, RoutedEventArgs e)
        {
            if (!string.IsNullOrEmpty(this._ModuleGuid) && !string.IsNullOrEmpty(this._VerbCode))//权限按钮
            {
                this.Visibility = System.Windows.Visibility.Collapsed;
                if (EHR.Common.Session.LoginUser.Permissions == null || EHR.Common.Session.LoginUser.Permissions.Where(c => c.VerbBaseGuid == this._ModuleGuid && c.VerbBaseCode == this._VerbCode).Count() <= 0)
                {
                    this.Visibility = System.Windows.Visibility.Collapsed;
                    //var sp = this.Parent as StackPanel;
                    //if (sp != null)
                    //{
                    //    sp.UpdateLayout();
                    //    ArrangeOverride(new Size(0, 0));
                    //}

                }
                else if (EHR.Common.Session.LoginUser.Permissions.Where(c => c.VerbBaseGuid == this._ModuleGuid && c.VerbBaseCode == this._VerbCode).Count() > 0)
                {
                    this.Visibility = System.Windows.Visibility.Visible;
                }
            }
        }
        

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            

            object tobj = App.Current.Resources["TIcon"];

            if (tobj != null && tobj is List<ImageSource>)
                this.NormalImage = ((List<ImageSource>)tobj)[ImageIndex];
            

        }

        public static readonly DependencyProperty normalImageProperty = DependencyProperty.Register("NormalImage", typeof(ImageSource), typeof(ImageButton), null);
        public ImageSource NormalImage
        {
            get { return (ImageSource)GetValue(normalImageProperty); }
            set { SetValue(normalImageProperty, value); }
        }



        public static readonly DependencyProperty TitleProperty = DependencyProperty.Register("IBTitle", typeof(string), typeof(ImageButton), null);
        public string IBTitle
        {
            get { return GetValue(TitleProperty).ToString(); }
            set { SetValue(TitleProperty, value); }
        }

        public static readonly DependencyProperty ImageIndexProperty = DependencyProperty.Register("ImageIndex", typeof(int), typeof(ImageButton), null);
        public int ImageIndex
        {
            get { return Convert.ToInt32(GetValue(ImageIndexProperty)); }
            set { SetValue(ImageIndexProperty, value); }
        }

        private string _ModuleGuid;

        public string ModuleGuid
        {
            get { return _ModuleGuid; }
            set { _ModuleGuid = value; }
        }

        private string _VerbCode;

        public string VerbCode
        {
            get { return _VerbCode; }
            set { _VerbCode = value; }
        }
        
        
    }
}
